from datetime import datetime, timedelta, date
from django import template
from django.contrib.auth.models import User
from django.db.models.aggregates import Count
from django.template.loader import render_to_string
from django.conf import settings

register = template.Library()

@register.simple_tag
def top_week():

    monday = datetime.today() - timedelta(days=datetime.today().weekday())
    users = User.objects.filter(
	comment__date__gte = date.today().replace(day=1)
    ).annotate(
        num_comments=Count('comment')
    ).order_by("-num_comments")[:5]

    return render_to_string(
        "%s/%s" % (settings.LIVE_ENGINE_TEMPLATE, "users/top_week.html"), {
            'users': users
        }
    )

@register.simple_tag
def user_panel(user):

    return render_to_string(
        "%s/%s" % (settings.LIVE_ENGINE_TEMPLATE, "users/panel.html"),
            {
            'user': user
        }
    )

